package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.CaiPu;


public class CaiPuRowMapper implements RowMapper<CaiPu>{

    private int type;

    public static final int CAIPU_AND_STR = 1;
    public static final int CAIPU_AND_UPDATE = 2;
    public static final int CAIPU_AND_STR_APP = 3;
    public static final int CAIPU_AND_FAVORITE_APP = 4;
    
    public CaiPuRowMapper() {
    }

    public CaiPuRowMapper(int type) {
        this.type = type;
    }

    @Override
    public CaiPu mapRow(ResultSet rs, int rowNum) throws SQLException {
        CaiPu cp = new CaiPu();
        switch (type) {
            case CAIPU_AND_STR:
                cp = parseCaiPu(new CaiPu(), rs);
                cp.setCx_name(rs.getString("cx_name"));
                cp.setNd_name(rs.getString("nd_name"));
                cp.setGy_name(rs.getString("gy_name"));
                cp.setKw_name(rs.getString("kw_name"));
                cp.setZl_names(rs.getString("zl_names"));
                cp.setFl_names(rs.getString("fl_names"));
                break;
            case CAIPU_AND_UPDATE:
                cp = parseCaiPu(new CaiPu(), rs);
                cp.setZl_names(rs.getString("zl_names"));
                cp.setFl_names(rs.getString("fl_names"));  
                break;
            case CAIPU_AND_STR_APP:
                cp = parseCaiPu(new CaiPu(), rs);
                cp.setNd_name(rs.getString("nd_name"));
                cp.setGy_name(rs.getString("gy_name"));
                cp.setKw_name(rs.getString("kw_name"));
                break; 
            case CAIPU_AND_FAVORITE_APP:
            	cp = parseCaiPu(new CaiPu(), rs);
                cp.setNd_name(rs.getString("nd_name"));
                cp.setGy_name(rs.getString("gy_name"));
                cp.setKw_name(rs.getString("kw_name"));
                cp.setUf_id(rs.getLong("uf_id"));
                break; 
            default:
                cp = parseCaiPu(new CaiPu(), rs);
        }
        return cp;
    }

    public static CaiPu parseCaiPu(CaiPu cp, ResultSet rs)
        throws SQLException {
        return parseCaiPu(cp, rs, null);
    }

    public static CaiPu parseCaiPu(CaiPu cp, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            cp.setCp_id(rs.getLong(su + "cp_id"));
        } catch (SQLException e) {
            cp.setCp_id(null);
        }
        try {
            cp.setCx_id(rs.getLong(su + "cx_id"));
        } catch (SQLException e) {
            cp.setCx_id(null);
        }
        try {
            cp.setNd_id(rs.getLong(su + "nd_id"));
        } catch (SQLException e) {
            cp.setNd_id(null);
        }
        try {
            cp.setGy_id(rs.getLong(su + "gy_id"));
        } catch (SQLException e) {
            cp.setGy_id(null);
        }
        try {
            cp.setKw_id(rs.getLong(su + "kw_id"));
        } catch (SQLException e) {
            cp.setKw_id(null);
        }
        try {
            cp.setCp_name(rs.getString(su + "cp_name"));
        } catch (SQLException e) {
            cp.setCp_name(null);
        }
        try {
            cp.setCp_desc(rs.getString(su + "cp_desc"));
        } catch (SQLException e) {
            cp.setCp_desc(null);
        }
        try {
            cp.setCp_img(rs.getString(su + "cp_img"));
        } catch (SQLException e) {
            cp.setCp_img(null);
        }
        try {
            cp.setCp_time(rs.getString(su + "cp_time"));
        } catch (SQLException e) {
            cp.setCp_time(null);
        }     
        try {
            cp.setCp_ptime(rs.getString(su + "cp_ptime"));
        } catch (SQLException e) {
            cp.setCp_ptime(null);
        }  
        try {
            cp.setCp_number(rs.getString(su + "cp_number"));
        } catch (SQLException e) {
            cp.setCp_number(null);
        }  
        return cp;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            CaiPu cp) {
        par.put("cp_id", cp.getCp_id());
        par.put("cx_id", cp.getCx_id());
        par.put("nd_id", cp.getNd_id());
        par.put("gy_id", cp.getGy_id());
        par.put("kw_id", cp.getKw_id());
        par.put("cp_name", cp.getCp_name());
        par.put("cp_desc", cp.getCp_desc());
        par.put("cp_img", cp.getCp_img());
        par.put("cp_time", cp.getCp_time());
        par.put("cp_ptime", cp.getCp_ptime());
        par.put("cp_number", cp.getCp_number());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            CaiPu cp) {
        args.put("cp_id", cp.getCp_id());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final CaiPu CP) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(CP),
                        new String[] { "cp_id" });
                parsePS(ps, CP);
                return ps;
            }

        };
    }

    private static String parseSQL(CaiPu cp) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_caipu(");
        int count = 0;
        if (null != cp.getCp_id()) {
            sql.append("cp_id,");
            count++;
        }
        if (null != cp.getCx_id()) {
            sql.append("cx_id,");
            count++;
        }
        if (null != cp.getNd_id()) {
            sql.append("nd_id,");
            count++;
        }
        if (null != cp.getGy_id()) {
            sql.append("gy_id,");
            count++;
        }
        if (null != cp.getKw_id()) {
            sql.append("kw_id,");
            count++;
        }
        if (null != cp.getCp_name()) {
            sql.append("cp_name,");
            count++;
        }
        if (null != cp.getCp_desc()) {
            sql.append("cp_desc,");
            count++;
        }
        if (null != cp.getCp_img()) {
            sql.append("cp_img,");
            count++;
        }
        if (null != cp.getCp_time()) {
            sql.append("cp_time,");
            count++;
        }
        if (null != cp.getCp_ptime()) {
            sql.append("cp_ptime,");
            count++;
        }
        if (null != cp.getCp_number()) {
            sql.append("cp_number,");
            count++;
        }

        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, CaiPu cp)
        throws SQLException {
        int count = 1;
        if (null != cp.getCp_id()) {
            ps.setLong(count++, cp.getCp_id());
        }
        if (null != cp.getCx_id()) {
            ps.setLong(count++, cp.getCx_id());
        }
        if (null != cp.getNd_id()) {
            ps.setLong(count++, cp.getNd_id());
        }
        if (null != cp.getGy_id()) {
            ps.setLong(count++, cp.getGy_id());
        }
        if (null != cp.getKw_id()) {
            ps.setLong(count++, cp.getKw_id());
        }
        if (null != cp.getCp_name()) {
            ps.setString(count++, cp.getCp_name());
        }        
        if (null != cp.getCp_desc()) {
            ps.setString(count++, cp.getCp_desc());
        } 
        if (null != cp.getCp_img()) {
            ps.setString(count++, cp.getCp_img());
        } 
        if (null != cp.getCp_time()) {
            ps.setString(count++, cp.getCp_time());
        } 
        if (null != cp.getCp_ptime()) {
            ps.setString(count++, cp.getCp_ptime());
        } 
        if (null != cp.getCp_number()) {
            ps.setString(count++, cp.getCp_number());
        } 

    }

}
